package com.mischa.doedeljas.client.ui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;

import com.jgoodies.forms.layout.CellConstraints;
import com.jgoodies.forms.layout.FormLayout;
import com.mischa.doedeljas.client.DoedelJas;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class ConnectionDialog 
	extends javax.swing.JDialog
	implements ActionListener
{
	/**
	 * 
	 */
	private static final long serialVersionUID = -8598979461025683425L;

	private DoedelJas mainForm;
	
	private JLabel serverLabel;
	private JTextField port;
	private JLabel gameIdLabel;
	private JTextField gameId;
	private JButton loginButton;
	private JTextField server;
	private JLabel portLabel;
	
	public ConnectionDialog(JFrame frame) {
		super(frame);
		initGUI();
	}
	
	public void setMainForm(DoedelJas mainForm)
	{
		this.mainForm = mainForm;	
	}
	
	private void initGUI() {
		try {
			FormLayout thisLayout = new FormLayout(
					"max(p;5dlu), 111dlu", 
					"max(p;5dlu), max(p;5dlu), max(p;5dlu), 13dlu");
			getContentPane().setLayout(thisLayout);
			this.setTitle("Connect to Doedeljas server");
			{
				serverLabel = new JLabel();
				getContentPane().add(serverLabel, new CellConstraints("1, 1, 1, 1, default, default"));
				serverLabel.setText("Server:");
			}
			{
				portLabel = new JLabel();
				getContentPane().add(portLabel, new CellConstraints("1, 2, 1, 1, default, default"));
				portLabel.setText("Port:");
			}
			{
				server = new JTextField();
				getContentPane().add(server, new CellConstraints("2, 1, 1, 1, default, default"));
				server.setText("localhost");
			}
			{
				port = new JTextField();
				getContentPane().add(port, new CellConstraints("2, 2, 1, 1, default, default"));
				port.setText("1234");
			}
			{
				loginButton = new JButton();
				getContentPane().add(loginButton, new CellConstraints("2, 4, 1, 1, default, default"));
				loginButton.setText("Connect");				
				loginButton.addActionListener(this);
			}
			{
				gameId = new JTextField();
				getContentPane().add(gameId, new CellConstraints("2, 3, 1, 1, default, default"));
				gameId.setText("0042");
			}
			{
				gameIdLabel = new JLabel();
				getContentPane().add(gameIdLabel, new CellConstraints("1, 3, 1, 1, default, default"));
				gameIdLabel.setText("Game ID:");
			}
			this.setSize(269, 132);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == loginButton)
		{
			if (mainForm != null)
			{
				try {
					mainForm.connect(server.getText(), Integer.parseInt(port.getText()), Integer.parseInt(gameId.getText()));
					this.setVisible(false);
				} catch (NumberFormatException exception) {
					JOptionPane.showMessageDialog(this, "Invalid number given for the port or game ID.\n" + exception.getMessage(), "Error connecting.", JOptionPane.ERROR_MESSAGE); 
				} catch (IOException exception) {
					JOptionPane.showMessageDialog(this, exception.getMessage(), "Error connecting.", JOptionPane.ERROR_MESSAGE);
				}
			}
		}
	}

}
